﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GNSCore.Exceptions;
using GNSCore.Helpers;
using GNSCore.Managers;
using GNSCore.Model;
using GNSCore.Repositories;

namespace GNSCore.Managers
{
    /// <summary>
    /// For saving, adding managing users in data base
    /// </summary>
    public class UserManger
    {
        private IRepository<User> _userRep;
        
        /// <summary>
        /// Default constructor for creating object graph
        /// </summary>
        public UserManger()
        {
        }

        /// <summary>
        /// Constr with concrete repository implementation paramenter
        /// </summary>
        /// <param name="_rep"></param>
        public UserManger(IRepository<User> _rep)
        {
            _userRep = _rep;
        }

        /// <summary>
        /// Returns user from data base
        /// </summary>
        /// <param name="userId">User identifier</param>
        /// <returns>User from data base</returns>
        public User GetUser(Guid userId)
        {
            User currentUser = _userRep.GetAll<User>().FirstOrDefault(us => us.UserId == userId);
            if(currentUser==null)
            {
                throw new UserNotFoundException(string.Format("User with id {0} was not fount in data base",userId));
            }
            return currentUser;
        }

        /// <summary>
        /// Activates specefied user 
        /// </summary>
        /// <param name="userId">User identifier</param>
        public void SetActiveToUser(Guid userId)
        {
            User currentUser = _userRep.GetAll<User>().FirstOrDefault(us => us.UserId == userId);
            if (currentUser == null)
            {
                throw new UserNotFoundException(string.Format("User with id {0} was not fount in data base", userId));
            }
            currentUser.Membership.IsApproved = true;
            _userRep.Save(currentUser);
        }

        /// <summary>
        /// Returns list of all users
        /// </summary>
        /// <returns>List of users</returns>
        public List<User> GetAllUsers()
        {
            return _userRep.GetAll<User>().ToList();
        }
    }
}